package UI;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

//@author A0103535R
/************************************ Abstract Class DSPanel ***************************/
public class DSPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private static Image _backgroundImageOne = null;
	private static Image _backgroundImageTwo = null;
	private static Image _backgroundImageThree = null;
	private static Image _backgroundImageFour = null;
	private static Image _backgroundImageFive = null;
	private static Image _backgroundImageSix = null;
	
	private Image _backgroundImage;
	private int _backgroundNum = 1;
	
	/**
	 * This is the constructor of DSPanel
	 * 
	 * @param bgn
	 * 			the no of background image chosen to paint
	 */
	public DSPanel(int bgn){
		init(bgn);
	}
	
	/**
	 * This function is used to initialize the appearance of DSPanel
	 *  
	 * @param bgn
	 * 			the no of background image chosen to paint
	 */
	private void init(int bgn){
		loadBackgroundImages();
		setOpaque(false);
		setBackground(new Color(223, 102, 102, 255));
		setBackgroundNum(bgn);
		changeBackgroundImage(bgn);
	}
	
	/**
	 * This function is used to load background image
	 */
	private void loadBackgroundImages(){
		if(_backgroundImageOne == null){
			_backgroundImageOne = loadBackgrounImage("background1.png");
		}
		
		if(_backgroundImageTwo == null){
			_backgroundImageTwo = loadBackgrounImage("background2.png");
		}
		
		if(_backgroundImageThree == null){
			_backgroundImageThree = loadBackgrounImage("background3.png");
		}
		
		if(_backgroundImageFour == null){
			_backgroundImageFour = loadBackgrounImage("background4.png");
		}
		
		if(_backgroundImageFive == null){
			_backgroundImageFive = loadBackgrounImage("background5.png");
		}
		
		if(_backgroundImageSix == null){
			_backgroundImageSix = loadBackgrounImage("background6.png");
		}
	}
	
	/**
	 * This function is used to load background image with path
	 * 
	 * @param url
	 * 			the path of image
	 * 
	 * @return image of background
	 */
	private Image loadBackgrounImage(String url){
		URL imageUrl = this.getClass().getClassLoader().getResource(url);
		ImageIcon img = new ImageIcon(imageUrl);
		
		return getScaledImage(img.getImage(),
				DSUILayout.FRAME_SIZE_WIDTH, 
				DSUILayout.FRAME_SIZE_HEIGHT);
	}
	
	/**
	 * This function is used to set background number
	 * 
	 * @param bgn
	 * 			the no of background image chosen to paint
	 */
	private void setBackgroundNum(int bgn){
		_backgroundNum = bgn;
	}
	
	/**
	 * This function is used to change background image
	 * 
	 * @return the no of background image chosen.
	 */
	public int changeBackground(){
		if(_backgroundNum < 6){
			_backgroundNum ++;
		}else{
			_backgroundNum = 1;
		}
		
		changeBackgroundImage(_backgroundNum);	
		return _backgroundNum;
	}
	
	/**
	 * This function is used to change background image
	 * 
	 * @param bgn
	 * 			the no of background image chosen to paint
	 */
	private void changeBackgroundImage(int bgn){
		switch (bgn) {
		case 1:
			_backgroundImage = _backgroundImageOne;
			break;
			
		case 2:
			_backgroundImage = _backgroundImageTwo;
			break;
			
		case 3:
			_backgroundImage = _backgroundImageThree;
			break;
			
		case 4:
			_backgroundImage = _backgroundImageFour;
			break;
			
		case 5:
			_backgroundImage = _backgroundImageFive;
			break;
			
		case 6:
			_backgroundImage = _backgroundImageSix;
			break;
			
		default:
			_backgroundImage = _backgroundImageOne;
			break;
		}
		repaint();
	}
	
	/**
	 * This function is used to paint background image on panel
	 * 
	 * @param g
	 * 			graphics of painting
	 */
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g; 
        g2d.drawImage(_backgroundImage, 0, 0, 
        		DSUILayout.FRAME_SIZE_WIDTH,
        		DSUILayout.FRAME_SIZE_HEIGHT, this);
        super.paint(g);
    }

	/**
	 * This function is used to get scaled image
	 * 
	 * @param srcImg
	 * 		image to scale
	 * @param w
	 * 		width of scaled image
	 * @param h
	 * 		height of scaled image
	 * 
	 * @return scaled image
	 */
	private Image getScaledImage(Image srcImg, int w, int h) {
		BufferedImage resizedImg = new BufferedImage(w, h,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = resizedImg.createGraphics();
		
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.setComposite(AlphaComposite.Src);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.WHITE);
		g2.fill(new RoundRectangle2D.Float(0, 0, w, h, 0, 0));
	    
		g2.setComposite(AlphaComposite.SrcAtop);
		g2.drawImage(srcImg, 0, 0, w, h, null);
		g2.dispose();
		return resizedImg;
	}
}
